package cn.xuewenbao.edu.controller;


import cn.xuewenbao.edu.entity.Article;
import cn.xuewenbao.edu.entity.vo.ArticleInfoVo;
import cn.xuewenbao.edu.entity.vo.ArticlePublishVo;
import cn.xuewenbao.edu.entity.vo.DraftVo;
import cn.xuewenbao.edu.service.ArticleService;
import org.jeecg.common.api.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author mafayu
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/edu/article")
@Api(tags = "文章表")
@Slf4j
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    //根据文章id查询文章基本信息
    @ApiOperation(value = "文章表-根据文章id查询文章基本信息")
    @GetMapping("/getArticleInfo/{articleId}/{userId}")
    public ArticleInfoVo getArticleInfo(@PathVariable String articleId, @PathVariable String userId) {
        return articleService.getArticleInfo(articleId, userId);
    }

    //获取文章列表
    @ApiOperation(value = "文章表-获取文章列表")
    @GetMapping("getAllArticle")
    public List<ArticleInfoVo> getArticleList() {
        List<ArticleInfoVo> list = articleService.listAllArticleInfo();
        return list;
    }

    //根据id排序获取文章用于首页显示
    @ApiOperation(value = "文章表-根据id获取前两篇文章")
    @GetMapping("getIndexArticle")
    public List<ArticleInfoVo> getIndexArticle() {
        List<ArticleInfoVo> list = articleService.getIndexArticleInfoVo();
        return list;
    }

    //根据访问量排序获取文章-用于轮播图
    @ApiOperation(value = "文章表-获取热门文章-用于轮播图")
    @GetMapping("getHotArticle")
    public List<ArticleInfoVo> getHotArticle() {
        List<ArticleInfoVo> list = articleService.getHotArticle();
        return list;
    }

    //添加文章
    @ApiOperation(value = "文章表-添加文章")
    @PostMapping("addArticle")
    public String addArticle(@RequestBody ArticlePublishVo articlePublishVo) {
        String id = articleService.addArticle(articlePublishVo);
        return id;
    }

    //文章最终发布
    //修改文章状态
    @ApiOperation(value = "文章最终发布")
    @PostMapping("publishArticle/{articleId}")
    public boolean publishArticle(@PathVariable String articleId) {
        Article article = new Article();
        article.setId(articleId);
        article.setStatus("Normal");
        return articleService.updateById(article);
    }

    //根据作者id获取文章
    @ApiOperation(value = "根据作者id获取文章")
    @GetMapping("getArticleList/{memberId}")
    public Result<List<ArticleInfoVo>> getAuthorArticleList(@PathVariable String memberId) {
        Result<List<ArticleInfoVo>> result = articleService.getArticleList(memberId);
        return result;
    }

    //根据id删除某篇文章或者草稿
    @ApiOperation(value = "根据id删除某篇文章或者草稿")
    @DeleteMapping("deleteArticle/{articleId}")
    public Result<?> deleteArticle(@PathVariable String articleId) {
        Result<?> result = articleService.deleteArticle(articleId);
        return result;
    }

    //根据id和作者id删除文章
    @ApiOperation(value = "根据id和作者id删除文章")
    @DeleteMapping("deleteOwnArticle/{articleId}/{userId}")
    public Result<?> deleteOwnArticle(@PathVariable String articleId,
                                      @PathVariable String userId) {
        Result<?> result = articleService.deleteOwnArticle(articleId, userId);
        return result;
    }

    //添加文章到草稿箱
    @ApiOperation(value = "文章表-添加文章到草稿箱")
    @PostMapping("addDraft")
    public Result<Boolean> addDraft(@RequestBody ArticlePublishVo articlePublishVo) {
        Result<Boolean> result = articleService.addDraft(articlePublishVo);
        return result;
    }

    //获取草稿箱
    @ApiOperation(value = "文章表-获取草稿箱")
    @GetMapping("getDraftList/{userId}")
    public Result<List<DraftVo>> getDraftList(@PathVariable String userId) {
        Result<List<DraftVo>> result = articleService.getDraftList(userId);
        return result;
    }

}

